from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config
from app.some_func import nohtml, init_error_views
from flask_session import Session
db = SQLAlchemy()
def create_app(config_name):
    app = Flask(__name__)
    # 初始化session设置
    app.config['SESSION_TYPE'] = 'filesystem'
    app.config['SESSION_FILE_THRESHOLD'] = 50
    Session(app)
    # 加载程序定义配置项。
    app.config.from_object(config[config_name])
    # Flask扩展的初始化操作
    db.init_app(app)
    # 注册前台蓝本
    from app.view.main import main as main_blueprint
    app.register_blueprint(main_blueprint)
    # 注册后台蓝本
    from app.view.admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/admin')
    # 将some_func中的三个函数传给jinja2
    app.jinja_env.globals.update(nohtml=nohtml)
    # 注册错误页面
    init_error_views(app)
    # 最后，返回Flask程序实例app 
    return app